package com.example.musicbox.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.example.musicbox.entity.dto.UserDto;
import com.example.musicbox.service.FileOperationService;
import com.example.musicbox.service.impl.FileOperationServiceImpl;
import com.example.musicbox.utils.GeneratorToken;
import com.example.musicbox.utils.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.nio.file.Path;
import java.util.Map;

/**
 * @author : zhy
 * @since : 2022/8/1
 * 测试controller
 */
@RequestMapping("/test")
@RestController
public class TestController {

    @Resource
    private GeneratorToken generatorToken;

    @Resource
    private FileOperationService fileOperationService;


    /**
     * @api {POST} /test/getToken 生成用户token
     * @apiVersion 1.0.0
     * @apiGroup 测试案例
     * @apiName getToken
     * @apiParam (请求体) {Number} uId 用户id
     * @apiParam (请求体) {String} userName 用户姓名
     * @apiParam (请求体) {String} uPhoto 用户头像
     * @apiParamExample 请求体示例
     * {
     *     "uId":12,
     *     "userName":"谢钰灿",
     *     "uPhoto":"123.jpg"
     * }
     * @apiSuccess (响应结果) {String} response
     * @apiSuccessExample 响应结果示例
     * {
     *     12fa923658e796f064b3fc6d6c61186e
     * }
     */
    @PostMapping("/getToken")
    public String getToken(@RequestBody UserDto userDto)
    {
        String token = generatorToken.getToken(userDto);
        return token;
    }


    /**
     * @api {POST} /test/getUserId 从redis中获取用户id
     * @apiVersion 1.0.0
     * @apiGroup 测试案例
     * @apiName getUserId
     * @apiParam (请求参数) {String} token
     * @apiParamExample 请求参数示例
     * token=3816562de4cfdd2b6fea89f254a2445e
     * @apiSuccess (响应结果) {Number} response
     * @apiSuccessExample 响应结果示例
     * {
     *     12
     * }
     */
    @PostMapping("/getUserId")
    public Integer getUserId(@RequestHeader String token)
    {
        Integer userId = generatorToken.getUserId(token);
        return userId;
    }


    /**
     * @api {POST} /test/uploadFile 文件上传
     * @apiVersion 1.0.0
     * @apiGroup 测试案例
     * @apiName uploadFile
     * @apiParam (请求参数) {Object} file
     * @apiParam (请求参数) {String} token
     * @apiParamExample 请求参数示例
     * file=null&token=ddVkEZUT
     * @apiSuccess (响应结果) {Object} response
     * @apiSuccessExample 响应结果示例
     * {}
     */
    @PostMapping("/uploadFile")
    public Result uploadFile(@RequestParam(value = "file") MultipartFile file, @RequestHeader String token)
    {
        //Result upload = fileOperationService.uploadFile(file, token);
        return null;
    }


    /**
     * @api {POST} /test/downloadFile 文件下载
     * @apiVersion 1.0.0
     * @apiGroup 测试案例
     * @apiName downloadFile
     * @apiParam (请求参数) {String} key
     * @apiParamExample 请求参数示例
     * key=Sm
     * @apiSuccess (响应结果) {Object} response
     * @apiSuccessExample 响应结果示例
     * {}
     */
    @PostMapping("/downloadFile")
    public Result downloadFile(@RequestBody Map map,@RequestHeader String token)
    {
        String key = (String) map.get("key");
        String path = (String) map.get("path");
        Result result = fileOperationService.downloadFile(key,path,token);
        return result;
    }


    /**
     * @api {POST} /test/deleteFile 删除文件
     * @apiVersion 1.0.0
     * @apiGroup 测试案例
     * @apiName deleteFile
     * @apiParam (请求体) {Object} requestBody
     * @apiParamExample 请求体示例
     * {}
     * @apiSuccess (响应结果) {Object} response
     * @apiSuccessExample 响应结果示例
     * {}
     */
    @PostMapping("/deleteFile")
    public Result deleteFile(@RequestBody Map map,@RequestHeader String token)
    {
        String key = (String) map.get("key");
        Result result = fileOperationService.deleteFile(key,token);
        return result;
    }

}
